package com.acmen.easyapi.model;

import java.text.DecimalFormat;

/**
 * @author acmen
 * 测试聚合报告
 */
public class TestReport {
    /**
     * id
     */
    private String id;
    /**
     * 所属测试ID
     */
    private String testId;
    /**
     * 接口ID
     */
    private String apiId;
    /**
     * 接口名称
     */
    private String apiName;

    public String path;//请求路径

    public String method;//请求方法
    /**
     * 样本总数
     */
    private int total;
    /**
     * 总花费时间
     */
    private long totalCost;
    /**
     * 最少花费时间
     */
    private long minCost = -1;
    /**
     * 最大花费时间
     */
    private long maxCost;
    /**
     * 平均花费时间
     */
    private long avgCost;
    /**
     * 发送流量（字节）
     */
    private long sendSize;
    /**
     * 接收流量（字节）
     */
    private long receiveSize;
    /**
     * 响应成功数
     */
    private int responseSuccessNum;

    /**
     * 业务成功数
     */
    private int businessSuccessNum;

    /**
     * 格式化字节
     * @param size
     */
    public static String formatByte(long size) {
        //获取到的size为：1705230
        int GB = 1024 * 1024 * 1024;//定义GB的计算常量
        int MB = 1024 * 1024;//定义MB的计算常量
        int KB = 1024;//定义KB的计算常量
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String resultSize = "";
        if (size / GB >= 1) {
            //如果当前Byte的值大于等于1GB
            resultSize = df.format(size / (float) GB) + "GB   ";
        } else if (size / MB >= 1) {
            //如果当前Byte的值大于等于1MB
            resultSize = df.format(size / (float) MB) + "MB   ";
        } else if (size / KB >= 1) {
            //如果当前Byte的值大于等于1KB
            resultSize = df.format(size / (float) KB) + "KB   ";
        } else {
            resultSize = size + "B   ";
        }
        return resultSize;
    }


    public String getSendSizeString() {
        return formatByte(sendSize);
    }
    public String getReceiveSizeString() {
        return formatByte(receiveSize);
    }

    public float getBusinessSuccessRate(){
        float rate = ((float) getBusinessSuccessNum()) / getTotal();
        return rate;
    }

    public String getId() {
        return id;
    }

    public TestReport setId(String id) {
        this.id = id;
        return this;
    }

    public String getTestId() {
        return testId;
    }

    public TestReport setTestId(String testId) {
        this.testId = testId;
        return this;
    }

    public String getApiId() {
        return apiId;
    }

    public TestReport setApiId(String apiId) {
        this.apiId = apiId;
        return this;
    }

    public String getApiName() {
        return apiName;
    }

    public TestReport setApiName(String apiName) {
        this.apiName = apiName;
        return this;
    }

    public String getPath() {
        return path;
    }

    public TestReport setPath(String path) {
        this.path = path;
        return this;
    }

    public String getMethod() {
        return method;
    }

    public TestReport setMethod(String method) {
        this.method = method;
        return this;
    }

    public int getTotal() {
        return total;
    }

    public TestReport setTotal(int total) {
        this.total = total;
        return this;
    }

    public long getTotalCost() {
        return totalCost;
    }

    public TestReport setTotalCost(long totalCost) {
        this.totalCost = totalCost;
        return this;
    }

    public long getMinCost() {
        return minCost;
    }

    public TestReport setMinCost(long minCost) {
        this.minCost = minCost;
        return this;
    }

    public long getMaxCost() {
        return maxCost;
    }

    public TestReport setMaxCost(long maxCost) {
        this.maxCost = maxCost;
        return this;
    }

    public long getAvgCost() {
        return avgCost;
    }

    public TestReport setAvgCost(long avgCost) {
        this.avgCost = avgCost;
        return this;
    }

    public long getSendSize() {
        return sendSize;
    }

    public TestReport setSendSize(long sendSize) {
        this.sendSize = sendSize;
        return this;
    }

    public long getReceiveSize() {
        return receiveSize;
    }

    public TestReport setReceiveSize(long receiveSize) {
        this.receiveSize = receiveSize;
        return this;
    }

    public int getResponseSuccessNum() {
        return responseSuccessNum;
    }

    public TestReport setResponseSuccessNum(int responseSuccessNum) {
        this.responseSuccessNum = responseSuccessNum;
        return this;
    }

    public int getBusinessSuccessNum() {
        return businessSuccessNum;
    }

    public TestReport setBusinessSuccessNum(int businessSuccessNum) {
        this.businessSuccessNum = businessSuccessNum;
        return this;
    }
}
